<?php if(!defined('PUBLIC_DIR')) die('No Access!');

Cnamespace('System');
class Request implements iRequest {
	// bools
	public function isSecure(){
		return ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') && (isset($_SERVER['SERVER_PORT_SECURE']) && $_SERVER['SERVER_PORT_SECURE'] === '1'));
	}
	public function isXmlHttpRequest(){
		return (isset($_SERVER['X-Requested-With']) && $_SERVER['X-Requested-With'] === 'XMLHttpRequest');
	}
	// values
	public function Host($name = null){
		return ($this->ServerHost() != '' ? $this->ServerHost() : $this->ServerAddress());
	}
	public function Method($name = null){
		return $_SERVER["REQUEST_METHOD"];
	}
	public function PathInfo($name = null){
		return (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '' ? '?' . $_SERVER['QUERY_STRING'] : '');
	}
	public function Referer($name = null){
		$referer = (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $this->ScriptVirtualPath());
		if($referer == $this->ScriptVirtualDir() || $referer == $this->ScriptVirtualPath())
			return '/';
		$referer = str_replace($this->PathInfo(), '', $referer);
		$referer = str_replace($this->ScriptVirtualPath(), '', $referer);
		if(strlen($this->ScriptVirtualDir()) > 1)
			$referer = str_replace($this->ScriptVirtualDir(), '', $referer);
		return $referer;
	}
	public function RemoteAddress($name = null){
		return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null);
	}
	public function RemotePort($name = null){
		return (isset($_SERVER['REMOTE_PORT']) ? $_SERVER['REMOTE_PORT'] : null);
	}
	public function ScriptExt($name = null){
		return substr(basename($_SERVER["SCRIPT_FILENAME"]), -strpos(basename($_SERVER["SCRIPT_FILENAME"]), '.'));
	}
	public function ScriptName($name = null){
		return basename($_SERVER["SCRIPT_FILENAME"]);
	}
	public function ScriptRealDir($name = null){
		return dirname($_SERVER["SCRIPT_FILENAME"]);
	}
	public function ScriptRealPath($name = null){
		return $_SERVER["SCRIPT_FILENAME"];
	}
	public function ScriptVirtualDir($name = null){
		return dirname($_SERVER["SCRIPT_NAME"]);
	}
	public function ScriptVirtualPath($name = null){
		return $_SERVER["SCRIPT_NAME"];
	}
	public function ServerAddress($name = null){
		return ($_SERVER["HTTP_HOST"]);
	}
	public function ServerAdmin($name = null){
		
	}
	public function ServerHost($name = null){
		return ($_SERVER['SERVER_NAME'] !== $_SERVER["HTTP_HOST"] ? $_SERVER['SERVER_NAME'] : '');
	}
	public function ServerPort($name = null){
		return $_SERVER['SERVER_PORT'];
	}
	public function ServerProtocol($name = null){
		return ($this->isSecure() ? 'https' : 'http');
	}
	public function Uri($name = null){
		return ($this->ServerProtocol() . '://' . $this->Host() . $this->ScriptVirtualPath() . $this->Referer() . $this->PathInfo());
	}
	//arrays
	public function AcceptableContentTypes($name = null, $bool = false){
		return array_values(explode(',', preg_replace('/\;q=[0-9\.]*/', '', $_SERVER['HTTP_ACCEPT'])));
	}
	public function Charsets($name = null, $bool = false){
		return array_values(explode(',', preg_replace('/\;q=[0-9\.]*/', '', $_SERVER['HTTP_ACCEPT_CHARSET'])));
	}
	public function Languages($name = null, $bool = false){
		return array_values(explode(',', preg_replace('/\;q=[0-9\.]*/', '', $_SERVER['HTTP_ACCEPT_LANGUAGE'])));
	}
	public function Encoding($name = null, $bool = false){
		return array_values(explode(',', preg_replace('/\;q=[0-9\.]*/', '', $_SERVER['HTTP_ACCEPT_ENCODING'])));
	}
	public function Parameter($name = null, $bool = false){
		return $_GET;
	}
	public function Cookie($name = null, $bool = false){
		return $_COOKIE;
	}
	public function Files($name = null, $bool = false){
		return $_FILES;
	}
}